#include <intrins.h>
#include "..\soft\program\SH88F516.h"
#include "..\soft\program\hardware.h"
#include "..\soft\program\incl.h"
#include "..\soft\program\display.h"
#include "..\soft\program\leddef.h"
#include "..\soft\program\extn_variable.h"

code uint8 DISP_DEF[38]= {Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9,			  		//0-9
							  CodeA, CodeB, CodeC, CodeD, CodeE, CodeF, CodeG, CodeH, CodeI, CodeJ,	  	//10-19
							  CodeK, CodeL, CodeM, CodeN, CodeO, CodeP, CodeQ, CodeR, CodeS, CodeT,	 	//20-29
							  CodeU, CodeV, CodeW, CodeX, CodeY, CodeZ};		  							//30-35


void write_tm1668(uint8 write_data)
 {
  register uint8 i;
	
  DSPSTB1 = 0;				//TM1668 STB = 0
  for(i = 0; i < 8; i++) 
   {			
    DSPCLK = 0;				//TM1668 CLK = 0
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    if(write_data & 0x01)		//send bit to TM1668;	from low bit to high bit
     { 
	  DSPDIO = 1;		
	 }
     else
	  {
	   DSPDIO = 0;
	  }
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
	DSPCLK = 1;				//TM1668 CLK = 1     	
	write_data >>= 1;			//next bit to be sended to TM1668	
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
   }
 }


void disp_init(void)
 {
  write_tm1668(DISPMODE);
  DSPSTB1 = 1;
 }



void display(void)
 {
  register uint8 i;

  write_tm1668(DISPSET);
  DSPSTB1 = 1;
  _nop_();
  write_tm1668(DISPADDR);
  for(i = 0; i < 7; i++)
   {
    write_tm1668(DISPBUFF[i]);
    write_tm1668(SignBlank);
   }
  DSPSTB1 = 1;
  _nop_();
  i = SYS_EEPROM.DspLevel;
  i |= 0x88;
  write_tm1668(i);		// display control; display on ; display bright lever
  DSPSTB1 = 1;
 }


void dspdigit(int32 templong, uint8 point, uint8 light)
{  
 	bool flag = 0; 	
 	uint8 i;   
 	uint32 temp ;

 	if(point > DISP_PT)	 
     point = 0;
 	point = DISP_PT - point;

 	if(templong < 0)
  	 temp = ~templong + 1;
     else
      temp = templong;

	Jbintobcd(temp, WINDOWS_LEN6);

  	for(i = 0; i < WINDOWS_LEN6; i++)
     {
      if((flag == 0) && (i < point) && (DISPBUFF[i] == 0))
	   DISPBUFF[i] = SignBlank;
	   else
	    {
         DISPBUFF[i] = DISP_DEF[DISPBUFF[i]];
	     flag = 1;
	    }
     }
  	if(templong < 0)
     DISPBUFF[0] = SignDash;

  	if(point != 5)
     DISPBUFF[point] |= SignPoint;

	DISPBUFF[6] = light;

  	display();		 		 
}



// string:		character content of display
// light:		indicator of display
void dspstr(uint8 * str, uint8 light)
{ 
 	uint8 i, k;
 	uint8 temp;
 	uint8 tempdisp;

 	for(i = 0; i < WINDOWS_LEN6; i++)
    {
	 temp = * str++;
	 if((temp >= 'a') && (temp <= 'z'))
	  tempdisp = DISP_DEF[temp - 87];
	  else if((temp >= 'A') && (temp <= 'Z'))		 
       tempdisp = DISP_DEF[temp - 55];
	    else
 	     {
	      if((temp >= '0') && (temp <= '9'))	 
           tempdisp = DISP_DEF[temp - 48];
		   else
		   {
		     switch(temp)
		    {
			 case ' ': tempdisp = SignBlank;
			          break;
			 case '-': tempdisp = SignDash;
			          break;
			 case '.': tempdisp = SignPoint;
			          break;
			 case '=': tempdisp = SignEq;
			          break;
			 case '_': tempdisp = Sign_S;
			          break;
             default: tempdisp = SignBlank;
                      break;
			 }
		   }
	     }
	 if(temp == 0)
	  {
		for( k = i; k < WINDOWS_LEN6; k++)
		 DISPBUFF[k] = SignBlank;
		 break;
	  }
	  else
       DISPBUFF[i] = tempdisp;
     }

	DISPBUFF[6] = light;

  	display();		 		 

}
